/**
 * 
 */
package com.csair.youth.web.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.constant.Constant;
import com.csair.youth.database.Permission;
import com.csair.youth.database.SysConfig;
import com.csair.youth.database.User;
import com.csair.youth.service.impl.PermissionServiceImpl;
import com.csair.youth.service.impl.SysConfigServiceImpl;
import com.csair.youth.service.impl.UserServiceImpl;

/**
 * @author mickey
 *
 */
public class LoginAction extends _BaseAction {
	private User user;
	private UserServiceImpl userService;
	private PermissionServiceImpl permissionService;
	private SysConfigServiceImpl sysConfigService;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public void setPermissionService(PermissionServiceImpl permissionService) {
		this.permissionService = permissionService;
	}

	public void setSysConfigService(SysConfigServiceImpl sysConfigService) {
		this.sysConfigService = sysConfigService;
	}

	public String execute(){
		return SUCCESS;
	}
	
	public String doLogin(){
		if(user == null || StringUtils.isEmpty(user.getLoginName()) || StringUtils.isEmpty(user.getPassword())){
			this.setMessage("用户名和密码都不能为空");
			return LOGIN;
		}
		try {
			if((user = userService.login(user.getLoginName(), user.getPassword())) != null){
				//成功
				SysConfig config = sysConfigService.getSysConfigByKey(Constant.REPO_STATUS_KEY);
				if(config == null){
					this.setMessage("资源库状态错误，不允许登录");
					return LOGIN;
				}
				List permissions = permissionService.getPermissionsByRoleId(user.getRoleId());
				if(Constant.REPO_STATUS_CLOSE.equals(config.getConfigValue())){
					//资源库关闭了，判断是否超级管理员，判断依据是是否有sysconfig的crud权限
					if(permissions == null || permissions.size() == 0){
						//不能登录
						this.setMessage("当前资源库已关闭，你不是超级管理员，不允许登录");
						return LOGIN;
					}
					else{
						boolean isAdmin = false;
						Iterator iter = permissions.iterator();
						while(iter.hasNext()){
							Permission p = (Permission)iter.next();
							if("/SysConfigList.action".equals(p.getUrl())){
								//OK
								isAdmin = true;
								break;
							}
						}
						if(!isAdmin){
							this.setMessage("当前资源库已关闭，你不是超级管理员，不允许登录");
							return LOGIN;
						}
					}
				}
				this.setSessionAttribute(Constant.USER_ID_SESSION_KEY, user.getId());
				this.setSessionAttribute(Constant.USER_NAME_SESSION_KEY, user.getName());
				this.setSessionAttribute(Constant.USER_ROLE_SESSION_KEY, user.getRoleId());
				this.setSessionAttribute(Constant.USER_PERMISSION_MAP_KEY, permissions);
				
				user.setLastLoginIp(this.getServletRequest().getRemoteAddr());
				user.setLastLoginTime(new Date());
				userService.updateUserLoginStatus(user);
				return "welcome";
			}
			else {
				this.setMessage("登录失败，用户名或密码错误");
				return LOGIN;
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setMessage("系统出错，请重试");
			return LOGIN;
		}
	}
	
	public String doLogout(){
		if(this.getSessionAttribute(Constant.USER_ID_SESSION_KEY) != null){
			this.setSessionAttribute(Constant.USER_ID_SESSION_KEY, null);
			this.setSessionAttribute(Constant.USER_NAME_SESSION_KEY, null);
			this.setSessionAttribute(Constant.USER_ROLE_SESSION_KEY, null);
			this.setSessionAttribute(Constant.USER_PERMISSION_MAP_KEY, null);
		}
		this.setMessage("已成功登出");
		return LOGIN;
	}
}
